﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Lidgren.Network;

namespace CodeGame.ServerObjects
{
    class PlayerManager
    {
        private List<Player> Players = new List<Player>();

        public PlayerManager()
        {

        }

        public void Add(Player ply)
        {
            this.Players.Add(ply);
        }

        public Player GetPlayer(string nick)
        {
            foreach (Player item in this.Players)
            {
                if (item.Nickname == nick)
                {
                    return item;
                }
            }
            throw new Exception("Player " + nick + " does not exist");
        }

        public Player GetPlayer(NetConnection conn)
        {
            foreach (Player item in this.Players)
            {
                if (item.Connection == conn)
                {
                    return item;
                }
            }
            throw new Exception("Player tied to NetConnection: " + conn.RemoteEndpoint + " does not exist");
        }

        public bool CheckPlayer(NetIncomingMessage msg)
        {
            return this.GetPlayer(msg.SenderConnection).AuthToken == msg.ReadString();
        }

    }
}
